package com.neusoft.neumooc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neusoft.core.common.BaseController;
import com.neusoft.core.common.conf.Params;
import com.neusoft.core.util.IDUtil;
import com.neusoft.core.util.Util;
import com.neusoft.neumooc.VO.OrderVO;
import com.neusoft.neumooc.VO.UserVO;
import com.neusoft.neumooc.service.OrderService;
import com.neusoft.neumooc.service.UserInfoService;

/**
 * @ClassName: OrderController
 * @Description: TODO 订单管理控制类
 * @author zls
 * @date 2016年11月1日
 */
@Controller
@RequestMapping(value = "/order")
public class OrderController extends BaseController {

	/**
	 * @Fields orderService : TODO 订单管理业务逻辑处理类
	 */
	@Autowired
	OrderService orderService;
	@Autowired
	UserInfoService userInfoService;

	/**
	 * @Title: list
	 * @Description: TODO 显示订单列表
	 * @param request
	 * @param response
	 * @param pageNo
	 *            起始页数
	 * @param pageSize
	 *            页面大小
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request,
			HttpServletResponse response, String pageNo, String pageSize)
			throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		int pageIndex = (Util.parseInt(pageNo) - 1) * Util.parseInt(pageSize);
		try {
			List<OrderVO> orderList = orderService.getOrderList(pageIndex,
					Util.parseInt(pageSize));
			int total = orderService.getOrderNum();
			retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
			retMap.put("orderList", orderList);
			retMap.put("total", total);
		} catch (Exception e) {
			log.error("显示订单列表有异常信息抛出" + e.getMessage(), e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
		}
		return retMap;
	}

	/**
	 * @Title: add
	 * @Description: TODO 增加订单
	 * @param request
	 * @param response
	 * @param ordervo
	 *            订单实体
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/add")
	@ResponseBody

	public Map<String, Object> add(HttpServletRequest request,
			HttpServletResponse response, OrderVO orderVO, UserVO userVO)
			throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			String id = IDUtil.getID();
			orderVO.setUserId(id);
			orderVO.setOrderStatus("0");
			orderVO.setPayStatus("0");
			int count = orderService.addOrder(orderVO);
			userVO.setUid(id);
			if (count > 0
					&& userInfoService.checkRepeat(userVO.getUserName()) != false
					&& userVO.getUserName() != "") {
				int number = userInfoService.insertUsers(userVO);
				if (number > 0) {
					retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
					retMap.put(Params.RET_MSG, "订单提交成功");
				}
			} 
			else {
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "订单提交失败,用户名不能为空,且名字不能重复");
			}


		} catch (Exception e) {
			log.error("提交订单有异常信息抛出" + e.getMessage(), e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "提交订单失败");
		} 

		
		return retMap;
	}

	/**
	 * @Title: paychange
	 * @Description: TODO 更改订单支付状态
	 * @param request
	 * @param response
	 * @param orderId
	 *            订单ID
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/paychange")
	@ResponseBody
	public Map<String, Object> payChange(HttpServletRequest request,
			HttpServletResponse response, String orderId) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			int count = orderService.changeOrderPay(orderId);
			if (count > 0) {
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_MSG, "支付成功");
			} else {
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "支付失败");
			}
		} catch (Exception e) {
			log.error("更改订单支付状态有异常信息抛出" + e.getMessage(), e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "支付失败");
		}
		return retMap;
	}

	/**
	 * @Title: orderChange
	 * @Description: TODO 更改订单发货状态
	 * @param request
	 * @param response
	 * @param orderId
	 *            订单ID
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/statuschange")
	@ResponseBody
	public Map<String, Object> statusChange(HttpServletRequest request,
			HttpServletResponse response, String orderId) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			int count = orderService.changeOrderStatus(orderId);
			if (count > 0) {
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_MSG, "发货成功");
			} else {
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "发货失败");
			}
		} catch (Exception e) {
			log.error("更改订单支付状态有异常信息抛出" + e.getMessage(), e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "发货失败");
		}
		return retMap;
	}

	/**
	 * @Title: delete
	 * @Description: TODO 删除订单
	 * @param request
	 * @param response
	 * @param orderId
	 *            订单ID
	 * @return Map
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, String orderId) throws Exception {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			int count = orderService.deleteOrder(orderId);
			if (count > 0) {
				retMap.put(Params.RET_CODE, Params.RET_CODE_SUCCESS);
				retMap.put(Params.RET_MSG, "删除成功");
			} else {
				retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
				retMap.put(Params.RET_MSG, "删除失败");
			}
		} catch (Exception e) {
			log.error("更改订单支付状态有异常信息抛出" + e.getMessage(), e.getCause());
			retMap.put(Params.RET_CODE, Params.RET_CODE_ERROR);
			retMap.put(Params.RET_MSG, "删除失败");
		}
		return retMap;
	}

}
